package com.sikadwafastdaisfondturbo.pretloan.weight.font;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

/**
 * NormalEditText
 * @Author wwdeng
 * @CreateTime: 2024/11/28 14:50
 * @Annotation：自定义输入框，监听焦点变化并上报
 */
public class NormalEditText extends AppCompatEditText implements TextWatcher, View.OnFocusChangeListener {

    // 是否触发粘贴事件
    private boolean isPaste = false;
    // 是否在未触发粘贴事件的情况下触发了编辑事件或者粘贴后修改了输入文案
    private boolean isEdit = false;

    // 记录触发粘贴事件后第一次的编辑事件
    private boolean afterPaste = false;

    private long startTime = 0;
    private long endTime = 0;

    //失去焦点回调
    private IInputFocusChangeListener mFocusListener = null;

    public NormalEditText(@NonNull Context context) {
        super(context);
        init();
    }

    public NormalEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NormalEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    public void setTypeface(@Nullable Typeface tf, int style) {
        super.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
    }

    private void init() {
        setFocusable(true);
        setFocusableInTouchMode(true);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    @Override
    public boolean onTextContextMenuItem(int id) {
        if (id == android.R.id.paste) {
            isPaste = true;
            afterPaste = true;
            // 粘贴后重置编辑事件
            isEdit = false;
        }
        return super.onTextContextMenuItem(id);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        // 粘贴后会同时触发一次afterTextChanged，此时不记录编辑事件
        if (afterPaste) afterPaste = false;
        // 未触发粘贴事件或者粘贴后文案有修改则记录编辑事件
        else isEdit = true;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (this.mFocusListener != null){
            this.mFocusListener.onFocusChange(v,hasFocus);
        }
        if (hasFocus) startTime = System.currentTimeMillis();
        else {
            endTime = System.currentTimeMillis();
            String content = getText().toString();
            long time = endTime - startTime;
            StringBuilder pe = new StringBuilder();
            if (isPaste) pe.append("P");
            if (isEdit) pe.append("E");
            String remark = content+"$$"+time+"$$"+pe.toString();
            // 上报埋点
            if (this.mFocusListener != null){
                this.mFocusListener.onCompleteAction(v,remark);
            }
        }
    }

    public void setInputFocusChangeListener(IInputFocusChangeListener listener){
        this.mFocusListener = listener;
    }

    public interface IInputFocusChangeListener{
        void onFocusChange(View v,boolean hasFocus);
        void onCompleteAction(View v, String remark);
    }
}
